/**
 * @name 公文处理类
 * @author Yang
 * @date 2018-07-17
 * @version 1.0.1
 */
var Apps= function () {

    var errors={
        'userNotNull':{
            success:false,
            message:"请选择提交的用户"
        },
        'radio':{
            success:false,
            message:"请选择用户"
        }
    }
    
    /**
     *
     */
    var indexHandler= function () {
        $('table').on("click","a.btn",function () {
            var id= $(this).data("id");
            $("#id").val(id);
            $("#addDepartment").modal('show');
        });
        $('#submitForm').validate({
            rules:{
                name:"required"
            },
            messages:{
                name:{
                    required:"必填"
                }
            },
            submitHandler:function (form) {
                var params={};
                params.name= $("#name").val();
                params.appId= $("#id").val();
                params= JSON.stringify(params);
                $.ajax({
                    type:"post",
                    url:"submit",
                    data:{"params":params},
                    dataType:"json",
                    success:function (data) {
                        $("#addDepartment").modal('hide');
                        console.log(data);
                        Common.alert(data);
                        if (data.success){
                            setTimeout(function () {
                                location.href='add.html?id='+data.data;
                            },1500);
                        } else {
                            setTimeout(function () {
                                location.reload(true);
                            },1500);
                        }
                    }
                })
            }
        })
    };

    /**
     * 保存数据的函数
     *
     */
    var saveHandler= function (goto) {
        var params={};
        params.appsId= $("#appsId").val();
        params.typeId= $('#typeId').val();
        var map={};
        $('input,textarea').each(function () {
            if (!$(this).attr("disabled")){
                if ($(this).attr('name')==undefined){
                    return;
                }
                if ($(this).attr('type')=="hidden"){
                    return;
                }
                if ($(this).hasClass('tao_canvas')){
                    return;
                }
                map[$(this).attr('name')]= $(this).val();
            }
        });
        var canvasName= $('.tao_canvas').attr('name');
        console.log(canvasName);
        var canvasHidden=$('input[name="'+canvasName+'_value"]');
        console.log(canvasHidden.length);
        if (canvasHidden.length>0){
            var images="";
            canvasHidden.each(function () {
                images+='<img src="'+$(this).val()+'"/>';
            })
            map[canvasName]=images;
        }
        var select=$('select,input[type=radio]:checked,input[type=checkbox]:checked');
        if (select.length>0){
            select.each(function () {
                if ($(this).attr("disabled")){
                    return;
                }
                if ($(this).attr('name')==undefined){
                    return;
                }
                map[$(this).attr('name')]=$(this).val();
            })
        }
        params.map= map;
        params= JSON.stringify(params);
        $.ajax({
            type:"post",
            url:"transect",
            data:{'params':params},
            dataType:"json",
            success:function (data) {
                Common.alert(data);
                if (data.success){
                    setTimeout(function () {
                        if (goto==null){
                            location.reload(true);
                        }else {
                            location.href="core.html?id="+$('#appsId').val();
                        }
                    },1500);
                }
            }
        })
    };

    /**
     *  办理公文的函数
     */
    var transectHandler= function () {
        $('table').addClass('table').addClass('table-bordered').addClass('table-striped');
        var editor= new Simditor({textarea:".ckeditor",
            defaultImage: "http://ozwpnu2pa.bkt.clouddn.com/a9.jpg",
            upload:{
                url : '${base}/admin/common/simditorFile', //文件上传的接口地址
                params: {"type":6}, //键值对,指定文件上传接口的额外参数,上传的时候随文件一起提交
                fileKey: 'file', //服务器端获取文件数据的参数名
                connectionCount: 3,
                leaveConfirm: '正在上传文件'
            }});
        Common.checkbox();
        var canvas= $('.tao_canvas');
        if (canvas.length==0){
            return;
        }
        canvas.click(function(){
            WritingPad.init($(this),base);
        });
        $(document).on("click",'img',function(){
            if ($(this).nextAll('.btn').attr("disabled")){
                return;
            }

            WritingPad.init($(this),base);
        });
        $('.ibox').on('click','a.save',function () {
            saveHandler();
        });
        $('.ibox').on("click",'a.goto',function () {
            saveHandler("goto");
        })
    };

    /**
     * 转交下一步的处理函数
     *
     */
    var coreHandler= function () {
       Common.checkbox();
       $('form').validate({
           submitHandler:function (form) {
               var userName= $('#userName').val();
               if (userName==null || userName==""){
                   Common.alert(errors.userNotNull);
                   return;
               }
               var params={};
               params.appsId= appsId;
               params.personId= $('#personId').val();
               params.type= $('input[name=audit]:checked').val();
               $.ajax({
                   type:"post",
                   url:"core",
                   data:params,
                   dataType:"json",
                   success:function (data) {
                       Common.alert(data);
                       if (data.success){
                           setTimeout(function () {
                               location.href="transect.html";
                           },15000)
                       }
                   }
               })
           }
       });

       $('.ibox').on("click",'button.user',function () {
           parent.layer.open({
               type:2,
               shadeClass:false,
               shade:0.8,
               area:['900px','650px'],
               btn:['确认'],
               yes:function(index,layers){
                   var iframe= layers.find('iframe');
                   var radio= iframe.contents().find('input[type=radio]:checked');
                   if (radio.length==0){
                       parent.layer.close(index);
                       Common.alert(errors.radio);
                       return;
                   }
                   var id= radio.data('id');
                   $('#personId').val(id);
                   $('#userName').val(radio.data('name'));
                   parent.layer.close(index);
               },
               content:"../user/index.html?check=ok"
           })
       })
    };


    return {
        index:function () {
            indexHandler();
        },
        transect:function () {
            transectHandler();
        },
        core:function () {
            coreHandler();
        }
    }
}();